package edu.pku.sei.transformation.editor.controller.commands.node;

import org.eclipse.gef.commands.Command;

import edu.pku.sei.modeler.model.core.MetaModelElement;
import edu.pku.sei.transformation.editor.model.Expression;
import edu.pku.sei.transformation.editor.model.Rule;
import edu.pku.sei.transformation.editor.model.variable.ObjectVariable;

public class ExpressionDeleteCommand extends Command {
	private Expression exp;
	private MetaModelElement father;

	public ExpressionDeleteCommand(Expression exp, MetaModelElement father) {
		this.exp = exp;
		this.father = father;
	}

	@Override
	public boolean canExecute() {
		if (this.exp == null || this.father == null)
			return false;
		return super.canExecute();
	}

	@Override
	public void execute() {
		if (father instanceof ObjectVariable) {
			ObjectVariable obj = (ObjectVariable) father;
			obj.getExpressions().remove(exp);
		} else if (father instanceof Rule) {
			Rule r = (Rule) father;
			r.getBody().remove(exp);
		}
	}

	@Override
	public void undo() {
		if (father instanceof ObjectVariable) {
			ObjectVariable obj = (ObjectVariable) father;
			obj.getExpressions().add(exp);
		} else if (father instanceof Rule) {
			Rule r = (Rule) father;
			r.getBody().add(exp);
		}
	}
}
